package com.atguigu.tingshu.account.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 账户充值延迟消息队列 -- 超时取消订单
 */
@Configuration
public class AccountRechargeDelayMessageConfig {
    /**
     * 延迟消息交换机
     * @return
     */
    @Bean("accountRechargeDelayExchange")
    public Exchange accountRechargeDelayExchange(){
        return ExchangeBuilder.directExchange("account_recharge_delay_exchange").build();
    }

    /**
     * 死信队列
     * @return
     */
    @Bean("accountRechargeDelayDeadQueue")
    public Queue accountRechargeDelayDeadQueue(){
        return QueueBuilder
                .durable("account_recharge_delay_dead_queue")
                .withArgument("x-dead-letter-exchange","account_recharge_delay_exchange")
                .withArgument("x-dead-letter-routing-key","account.recharge.normal")
                .build();
    }

    /**
     * 正常队列
     * @return
     */
    @Bean("accountRechargeDelayQueue")
    public Queue accountRechargeDelayQueue(){
        return QueueBuilder
                .durable("account_recharge_delay_queue")
                .build();
    }

    /**
     * 死信队列绑定
     * @param accountRechargeDelayExchange
     * @param accountRechargeDelayDeadQueue
     * @return
     */
    @Bean
    public Binding accountRechargeDelayDeadQueueBinding(@Qualifier("accountRechargeDelayExchange") Exchange accountRechargeDelayExchange,
                                                        @Qualifier("accountRechargeDelayDeadQueue") Queue accountRechargeDelayDeadQueue){
        return BindingBuilder
                .bind(accountRechargeDelayDeadQueue)
                .to(accountRechargeDelayExchange)
                .with("account.recharge.dead")
                .noargs();
    }

    /**
     * 正常队列绑定
     * @param accountRechargeDelayExchange
     * @param accountRechargeDelayQueue
     * @return
     */
    @Bean
    public Binding accountRechargeDelayQueueBinding(@Qualifier("accountRechargeDelayExchange") Exchange accountRechargeDelayExchange,
                                                        @Qualifier("accountRechargeDelayQueue") Queue accountRechargeDelayQueue){
        return BindingBuilder
                .bind(accountRechargeDelayQueue)
                .to(accountRechargeDelayExchange)
                .with("account.recharge.normal")
                .noargs();
    }

}
